package com.hrms.controller.admin;

import com.hrms.entity.Customer;
import com.hrms.entity.User;
import com.hrms.http.ResponseResult;
import com.hrms.service.CustomerService;
import com.hrms.service.UserService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Transactional
@RestController
@RequestMapping("/admin/usermanage/edituser")
public class AdminEditUserController {
    // 管理员用户信息修改

    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/init")
    public ResponseResult<List<Map<String, Object>>> init() {
        // 初始化
        List<Map<String, Object>> list = userService.selectAll();
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/update")
    public ResponseResult<List<Object>> update(@RequestBody EntityUtil entity) {
        // 修改用户信息
        User user = new User();
        user.setUser_id(entity.getUser_id());
        user.setUser_name(entity.getUser_name());
        user.setUser_password(entity.getUser_password());

        Customer customer = new Customer();
        customer.setCustomer_id(userService.selectCustomerId(user));
        customer.setCustomer_phone(entity.getCustomer_phone());

        // 检查账号是否离线
        User userBefore = new User();
        userBefore.setUser_id(entity.getUser_id());
        userBefore.setUser_status("离线");

        if(!userService.checkStatue(userBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 账号名已被使用
        if(userService.nameExist(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_user_name", null);
        }

        // 修改user
        if(!userService.updatebyAdmin(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改电话号码
        if(!Objects.equals(entity.getCustomer_phone(), "")) {
            if(!customerService.updatePhone(customer)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/delete")
    public ResponseResult<List<Object>> delete(@RequestBody EntityUtil entity) {
        // 删除账号

        // 检查账号是否离线
        User userBefore = new User();
        userBefore.setUser_id(entity.getUser_id());
        userBefore.setUser_status("离线");

        if(!userService.checkStatue(userBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        User userAfter = new User();
        userAfter.setUser_id(entity.getUser_id());
        userAfter.setUser_status("已注销");

        if(!userService.updateStatue(userAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/logout")
    public ResponseResult<List<Object>> logout(@RequestBody EntityUtil entity) {
        // 注销连接

        // 检查账号是否在线
        User userBefore = new User();
        userBefore.setUser_id(entity.getUser_id());
        userBefore.setUser_status("在线");

        if(!userService.checkStatue(userBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        User userAfter = new User();
        userAfter.setUser_id(entity.getUser_id());
        userAfter.setUser_status("离线");

        if(!userService.updateStatue(userAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/recover")
    public ResponseResult<List<Object>> recover(@RequestBody EntityUtil entity) {
        // 恢复账号

        // 检查账号是否已注销
        User userBefore = new User();
        userBefore.setUser_id(entity.getUser_id());
        userBefore.setUser_status("已注销");

        if(!userService.checkStatue(userBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        User userAfter = new User();
        userAfter.setUser_id(entity.getUser_id());
        userAfter.setUser_status("离线");

        if(!userService.updateStatue(userAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
